package com.kzj.mls.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.kzj.mls.R;
import com.kzj.mls.beans.BookDateTime;
import com.kzj.mls.beans.ScheduleBean;
import com.kzj.mls.beans.ServiceBookBean;
import com.kzj.mls.beans.TimeBean;
import com.kzj.mls.beans.TimeListBean;
import com.kzj.mls.global.App;
import com.kzj.mls.global.Constant;
import com.kzj.mls.recyclerCard.basic.MaterialListAdapter;
import com.kzj.mls.recyclerCard.basic.MaterialListView;
import com.kzj.mls.recyclerCard.card.other.ChooseLeftTimeCard;
import com.kzj.mls.recyclerCard.card.other.ChooseRightTimeCard;
import com.kzj.mls.recyclerCard.cardView.ChooseTimeLeftCardView;
import com.kzj.mls.request.BaseResponse;
import com.kzj.mls.request.ICallBackForRequestServer;
import com.kzj.mls.request.RequestServerManager;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by xyg on 2015/11/5.
 */
public class ChooseTimeActivity extends GBaseActivity implements View.OnClickListener {

    private MaterialListView material_listview_date;
    private MaterialListView material_listview_time;
    public static String TagDateList="TagDateList";
    public static String TagTimeList="TagTimeList";
    public String sid;
    public List<ServiceBookBean.ServiceTimeEntity> timeList;
    public List<String> dataList;
    private TextView textView_finish;
    private TextView textView_cancel;
    private BookDateTime bookDateTime;
    public static String bookDateTimeTag="bookDateTimeTag";
    private List<ScheduleBean> scheduleBeanList;
    private int leftCardposition=0;
    private int rightCardposition=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentViewFor720_1280(R.layout.activity_choose_time);
        findViewByIds();
        initDatas();
       // fillArrayInListView();
        TaskGetTimeList();
    }

    @Override
    protected void findViewByIds() {
        material_listview_date= (MaterialListView) findViewById(R.id.material_listview_left);
        material_listview_time= (MaterialListView) findViewById(R.id.material_listview_right);
        textView_finish= (TextView) findViewById(R.id.textView_finish);textView_finish.setOnClickListener(this);
        textView_cancel= (TextView) findViewById(R.id.textView_cancel);textView_cancel.setOnClickListener(this);
    }

    @Override
    protected void initDatas() {
        setTopTitle("排班列表");
        setLeftImg(View.VISIBLE, R.drawable.back);
        sid=getIntent().getStringExtra(ServiceOrderActivity.TagSID);
        dataList= (List<String>) getIntent().getSerializableExtra(TagDateList);
        timeList= (List<ServiceBookBean.ServiceTimeEntity>) getIntent().getSerializableExtra(TagTimeList);
        bookDateTime= (BookDateTime) getIntent().getSerializableExtra(bookDateTimeTag);
        EventBus.getDefault().register(this);
    }

    private void refreshListview() {
        if (scheduleBeanList!=null)
        {
            for (int i = 0; i < scheduleBeanList.size(); i++) {
                ChooseLeftTimeCard chooseLeftTimeCard = new ChooseLeftTimeCard(this);
                chooseLeftTimeCard.setScheduleBean(scheduleBeanList.get(i));
                material_listview_date.add(chooseLeftTimeCard);
                if (i == 0) {
                    chooseLeftTimeCard.setSelected(true);
                    for (int j = 0; j< scheduleBeanList.get(i).getHour().size(); j++) {
                        ChooseRightTimeCard chooseRightTimeCard = new ChooseRightTimeCard(this);

                        chooseRightTimeCard.setHourEntity(scheduleBeanList.get(i).getHour().get(j));

                        material_listview_time.add(chooseRightTimeCard);
                    }
                }


            }


        }
    }

    @Override
    protected void fillArrayInListView() {


    }

    public void onEventMainThread(ChooseLeftTimeCard chooseLeftTimeCard) {
        material_listview_time.clear();
        for (int i=0;i<material_listview_date.getAdapter().getItemCount();i++)
        {
            ChooseLeftTimeCard chooseLeftTimeCard_temp= (ChooseLeftTimeCard) ((MaterialListAdapter)material_listview_date.getAdapter()).getCard(i);
            chooseLeftTimeCard_temp.setSelected(false);
        }
        leftCardposition=((MaterialListAdapter)material_listview_date.getAdapter()).getPosition(chooseLeftTimeCard);
        for (int j = 0; j< scheduleBeanList.get(leftCardposition).getHour().size(); j++) {
            ChooseRightTimeCard chooseRightTimeCard = new ChooseRightTimeCard(this);

            chooseRightTimeCard.setHourEntity(scheduleBeanList.get(leftCardposition).getHour().get(j));

            material_listview_time.add(chooseRightTimeCard);
        }
        chooseLeftTimeCard.setSelected(true);

        material_listview_date.getAdapter().notifyDataSetChanged();
    }
    public void onEventMainThread(ChooseRightTimeCard chooseRightTimeCard) {

        rightCardposition=((MaterialListAdapter)material_listview_time.getAdapter()).getPosition(chooseRightTimeCard);
        ScheduleBean scheduleBean=scheduleBeanList.get(leftCardposition);

        TaskUpdateTimeList(scheduleBean.getDate_id()
                ,scheduleBean.getHour().get(rightCardposition).getTime_id()+"",
                scheduleBean.getHour().get(rightCardposition).getWeekid(),
                scheduleBean.getHour().get(rightCardposition).getMonth_id()
                ,chooseRightTimeCard);
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.textView_finish:
                if (bookDateTime!=null && bookDateTime.getBooking_time()!=null) {
                    finish();
                    EventBus.getDefault().post(bookDateTime);
                }else
                {
                    finish();
                }
                break;
            case R.id.textView_cancel:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void TaskGetTimeList() {
        RequestServerManager.getInstance().handleMethod(this, material_listview_date, null, false,
                Constant.getRootUrl() + Constant.Action.Action_Schedule, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_Schedule), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {

                        material_listview_time.clear();
                        List<ScheduleBean> scheduleBeanList_server = JSON.parseArray(result.getRetval().toString(),ScheduleBean.class);
                        scheduleBeanList=scheduleBeanList_server;
                        refreshListview();

                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }
    private void TaskUpdateTimeList(String date_id,String time_id,String weekid,String month_id,final ChooseRightTimeCard chooseRightTimeCard) {
        RequestServerManager.getInstance().handleMethod(this, null, null, false,
                Constant.getRootUrl() + Constant.Action.Action_SetSchedule, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_SetSchedule,date_id,time_id,weekid,month_id), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {

                        rightCardposition=((MaterialListAdapter)material_listview_time.getAdapter()).getPosition(chooseRightTimeCard);
                        if (chooseRightTimeCard.getHourEntity().getIsset()) {
                            chooseRightTimeCard.getHourEntity().setIsset(false);
                        }else
                        {
                            chooseRightTimeCard.getHourEntity().setIsset(true);
                        }

                        material_listview_time.getAdapter().notifyDataSetChanged();


                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName()); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
        MobclickAgent.onResume(this);          //统计时长
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName()); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息
        MobclickAgent.onPause(this);
    }
}
